package ysq.xyg.ticket.controller;

import org.springframework.ui.Model;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.ticket.model.po.TicketProvinces;
import ysq.xyg.ticket.model.vo.TicketProvincesVo;
import ysq.xyg.ticket.service.TicketProvincesService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 票卷模块-省市区controller层
 *
 * @author shengQiang yu
 * @date 2019-10-23 16:06:37
 */
@RestController
public class TicketProvincesController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(TicketProvincesController.class);
    @Resource
    private TicketProvincesService ticketProvincesService;

    private TicketProvinces ticketProvinces;
    @RequestMapping("/ticket/*")
    public Model goPage(Model model) {
        return model;
    }
    /**
     * 分页查询
     *
     * @param ticketProvincesVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    @BaseLog(module = "票卷模块",className = "省市区",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/ticketProvinces/page",method = RequestMethod.POST)
    public ReturnEntity page(TicketProvincesVo ticketProvincesVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = ticketProvincesService.page(ticketProvincesVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 集合查询
     *
     * @param ticketProvincesVo 对象
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    @BaseLog(module = "票卷模块",className = "省市区",methods = "集合查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/ticketProvinces/list",method = RequestMethod.POST)
    public ReturnEntity list(TicketProvincesVo ticketProvincesVo){
        ReturnEntity returnEntity = ticketProvincesService.list(ticketProvincesVo);
        return returnEntity;
    }
    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    @RequestMapping(value = "/ticketProvinces/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id,String parentId){
        ModelAndView view = new ModelAndView("/ticket/ticketProvincesUpdateOrSave");
        TicketProvinces ticketProvinces=new TicketProvinces();
        if(StrUtil.isNotEmpty(id)){
            ticketProvinces = ticketProvincesService.selectByPrimaryKey(id);
        }
        if(StrUtil.isNotEmpty(parentId)){
            TicketProvinces parentTicketProvinces = ticketProvincesService.selectByPrimaryKey(parentId);
            if(parentTicketProvinces!=null){
                ticketProvinces.setParentName(parentTicketProvinces.getName());
                ticketProvinces.setParentId(parentId);
                if(StrUtil.isNotEmpty(parentTicketProvinces.getProvincesType())){
                    ticketProvinces.setProvincesType((Integer.parseInt(parentTicketProvinces.getProvincesType())+1)+"");
                }
            }
        }
        view.addObject("ticketProvinces", ticketProvinces);
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    @BaseLog(module = "票卷模块",className = "省市区",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/ticketProvinces/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        List<TicketProvinces> list=ticketProvincesService.acceptList(new TicketProvincesVo(id));
        if(list!=null&&list.size()>0){
            return  new ReturnEntity(BaseConstant.FAIL,"请先删除下级内容！");
        }else{
            return ticketProvincesService.delete(id,acceptCurrentUser(request));
        }
    }

    /**
     * 添加或修改
     *
     * @param ticketProvincesVo Vo对象
     * @author shengQiang yu
     * @date 2019-10-23 16:06:37
     */
    @BaseLog(module = "票卷模块",className = "省市区",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/ticketProvinces/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,TicketProvincesVo ticketProvincesVo){
        return ticketProvincesService.addOrUpdate(ticketProvincesVo,acceptCurrentUser(request));
    }

    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    @RequestMapping(value = "/ticketProvinces/acceptSelect",method = RequestMethod.POST)
    public ReturnEntity acceptSelect(String code,TicketProvincesVo ticketProvincesVo){
        return ticketProvincesService.acceptSelect(code,ticketProvincesVo);
    }
}